/*
 *@author zg created
 *
 *
 *
 */
#include "thread_pool.h"
#define buff_size 4096
// ANSI escape sequences for color
#define COLOR_RESET "\x1b[0m" // 重置颜色
#define COLOR_BLUE "\x1b[34m"  // 蓝色表示目录
#define COLOR_GREEN "\x1b[32m" // 绿色表示可执行文件
#define COLOR_MAGENTA "\x1b[35m" // 洋红色表示链接文件
#define COLOR_CYAN "\x1b[36m"  // 青色表示有名管道
#define COLOR_YELLOW "\x1b[33m" // 黄色表示字符设备
#define COLOR_RED "\x1b[31m"   // 红色表示块设备
                               // 白色表示普通文件 (为了兼容客户端主题，故不做处理)
#define COLOR_GRAY "\x1b[90m"  // 灰色表示其他类型文件 
                               
// Function to print the file name with appropriate color
void print_entry_with_color(const char* filename, struct stat* sb, char* buff) {
    char tmp_buffer[256]; //用于将文件名染色后临时存储
    if (S_ISDIR(sb->st_mode)) {
        snprintf(tmp_buffer, sizeof(tmp_buffer), COLOR_BLUE "%s/\t" COLOR_RESET, filename);
        strcat(buff, tmp_buffer); // Directory
    } else if (S_ISREG(sb->st_mode)) {
        if (sb->st_mode & S_IXUSR) { // Check if user can execute
            // Executable regular file
            snprintf(tmp_buffer, sizeof(tmp_buffer), COLOR_GREEN "%s\t" COLOR_RESET, filename);
            strcat(buff, tmp_buffer);
        } else {
            // Non-executable regular file
            snprintf(tmp_buffer, sizeof(tmp_buffer), "%s\t", filename);
            strcat(buff, tmp_buffer);
        }
    } else if (S_ISLNK(sb->st_mode)) {
        // Symbolic link
        snprintf(tmp_buffer, sizeof(tmp_buffer), COLOR_MAGENTA "%s@\t" COLOR_RESET, filename);
        strcat(buff, tmp_buffer);
    } else if (S_ISFIFO(sb->st_mode)) {
        // Named pipe
        snprintf(tmp_buffer, sizeof(tmp_buffer), COLOR_CYAN "%s\t" COLOR_RESET, filename);
        strcat(buff, tmp_buffer);
    } else if (S_ISBLK(sb->st_mode)) {
        // Block special file
        snprintf(tmp_buffer, sizeof(tmp_buffer), COLOR_RED "%s\t" COLOR_RESET, filename);
        strcat(buff, tmp_buffer);
    } else if (S_ISCHR(sb->st_mode)) {
        // Character special file
        snprintf(tmp_buffer, sizeof(tmp_buffer), COLOR_YELLOW "%s\t" COLOR_RESET, filename);
        strcat(buff, tmp_buffer);
    } else {
        // Other types
        snprintf(tmp_buffer, sizeof(tmp_buffer), COLOR_GRAY "%s\t" COLOR_RESET, filename);
        strcat(buff, tmp_buffer);
    }
}

//char buff[buff_size] = {0};

int lsCommand(task_t * task)
{
    printf("lsCommand\n");
    //存储ls结果
    char buff[buff_size] = {0};
    
    //获取当前目录path_id
    int uid;      //用户id
    int path_id;  //当前目录id
    mkdir_get_dir_id(task->sp, task->name, &uid, &path_id);
    printf("uid:%d, path_id:%d\n", uid, path_id);

    //获取目录下文件
    get_ls(task->sp, path_id, uid, buff);
    //将处理好的条目发送给客户端
    printf("%s\n",buff);
    sendn(task->peerfd, buff, strlen(buff));
    printf("send over.\n");








    ////初始化buff
    //memset(buff, 0, buff_size);
    //printf("execute ls command.\n");
    //DIR * dir;
    //struct dirent* entry; 
    //struct stat sb;
    //int entry_len = 0; // 用来统计条目总长度
    //
    //// 打开当前文件目录
    //dir = opendir(".");
    //ERROR_CHECK(dir, NULL, "opendir");        
    //
    //// 读取目录条目并获取所有条目长度总和
    //while ((entry = readdir(dir)) != NULL) {
    //    if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
    //        continue; // 跳过当前目录和父目录条目
    //    }
    //    // 统计所有条目的长度并以水平制表符'\t'作为分隔
    //    entry_len += strlen(entry->d_name) + 1;
    //}
    //printf("entry_len: %d\n", entry_len);// 仅用于调试
    //// 创建相应大小的字符串数组

    //// 重置目录流
    //rewinddir(dir);
    //while ((entry = readdir(dir)) != NULL) {
    //    if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
    //        continue; // 跳过当前目录和父目录条目
    //    }
    //    // 获取文件的状态信息
    //    if (lstat(entry->d_name, &sb) == -1) {
    //        perror("Failed to stat file");
    //        continue;
    //    }
    //    // 将条目存储到一个buff中 再将buff传送给客户端。
    //    print_entry_with_color(entry->d_name, &sb, buff);
    //}
    //// 将处理好的条目发送给客户端
    ////printf("%s\n",buff);
    //sendn(task->peerfd, buff, sizeof(buff));
    //printf("send over.\n");

    //// 关闭当前目录流
    //closedir(dir);

    return 0;
}

